
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;
using System.Text;

using Swaf;
using Swaf.BizObj;
using Swaf.BizRule;
using Swaf.Container;

namespace Approval
{
	public class TemplateBizRule : Syrinx.BaseBizRules
	{
		public string getApprovalTemplate(IApplication app, IRuleContext context, IBizObj bizObj)
		{
			try
			{
				string templates = "";
				if (bizObj != null)
				{
					//if (bizObj.get("selectedObjectDataId").Equals(Swaf.BGuid.Guid.Empty))
					//    bizObj.put("state", 5);
					string strObj = bizObj.get("rawbizObj").ToString();
					//IBizObjSource src = app.bizObjMgr.createSource(SourceType.BasicSource, strObj) as IBizObjSource;
					//IBizObj thebizobj = src.getList(bizObj.get("approvalSet.objectType").ToString(), null)[0];
					if (strObj.Contains(string.Format("id=\"{0}\"", bizObj.get("selectedObjectDataId").ToString())))
					{
						string state = bizObj.get("state").ToString();
						if ("0".Equals(state))
							return "Approval.Gui.AspNet.BasicApprovalDisplay";
						else if ("1".Equals(state))
							return "Approval.Gui.AspNet.ApprovalStateDisplay";
						else if ("2".Equals(state))
							return "Approval.Gui.AspNet.ApprovalDeniedDisplay";
					}
				}
				return templates;
			}
			catch (Exception e)
			{
				throw new ApprovalException("error executin getApprovalTemplate.", e);
			}
		}
		public string getApprovalSetTemplate(IApplication app, IRuleContext context, IBizObj bizObj)
		{
			if (bizObj == null)
				return "Approval.Gui.AspNet.ApprovalSetDisplay";
			else
			{
				IResults rc = app.ruleMgr.execute("getApprovers", bizObj);
				bizObj.put("approvers", rc.CallResults as IBizObjList);
				return "Approval.Gui.AspNet.ApproverListDisplay";
			}
		}
	}
}
